package com.hydra.framework.router;

import android.content.Context;
import android.os.Bundle;
import android.os.Parcelable;

import com.hydra.framework.thread.ThreadBus;

import java.io.Serializable;

/**
 * Created by Hydra on 2017/7/29.
 *
 * 后面考虑支持parcelable
 */
public class RouterIntent {

    public String url;

    //这里就用Android自己的bundle，不自定义了，因为基本都是跳转到activity的
    //自定义的参数也不支持
    public Bundle bundle;

    public int intentFlags, extraFlags;

    public int enterAnim, exitAnim;

    public int requestCode;

    public boolean isForResult;

    public Context from;

    //针对于from是activity的类型的，为什么要加在这里呢
    //因为如果有interceptor的话，经常会弹出一些对话框，弹出对话框就需要from context的存在
    //如果不加在intent中，就会出现from已经finish，但是对话框需要弹出
    //或者会出现intent被拦截了，但是from已经finish了，这个时候就会出现一个中间态,会有很多bug
    public boolean finish;

    public RouterIntent(String url) {
        isForResult = false;
        exitAnim = -1;
        enterAnim = -1;

        bundle = new Bundle();

        parseUrl(url);
    }

    private void parseUrl(String url) {
        // TODO: 2017/8/5  
        this.url = url;
    }

    public RouterIntent from(Context context) {
        this.from = context;

        return this;
    }

    public RouterIntent finish(boolean finish) {
        this.finish = finish;

        return this;
    }

    public RouterIntent withString(String key, String value) {
        bundle.putString(key, value);

        return this;
    }

    public RouterIntent withInt(String key, int value) {
        bundle.putInt(key, value);

        return this;
    }

    public RouterIntent withLong(String key, long value) {
        bundle.putLong(key, value);

        return this;
    }

    public RouterIntent withSerializable(String key, Serializable value) {
        bundle.putSerializable(key, value);

        return this;
    }

    //还可以添加更多啦
    public RouterIntent withParcelable(String key, Parcelable value) {
        bundle.putParcelable(key, value);

        return this;
    }

    public RouterIntent withIntentFlag(int intentFlag) {
        intentFlags |= intentFlag;

        return this;
    }

    public RouterIntent withExtraFlag(int extraFlag) {
        extraFlags |= extraFlag;

        return this;
    }

    public RouterIntent withBundle(String key, Bundle bundle) {
        this.bundle.putAll(bundle);

        return this;
    }

    public RouterIntent forResult(boolean forResult) {
        isForResult = forResult;

        return this;
    }

    public RouterIntent requestCode(int requestCode) {
        this.requestCode = requestCode;

        return this;
    }

    public RouterIntent anim(int exitAnim, int enterAnim) {
        this.exitAnim = exitAnim;
        this.enterAnim = enterAnim;

        return this;
    }

    public void go() {
        ThreadBus.callThreadSafe(ThreadBus.Main, new Runnable() {
            @Override
            public void run() {
                JRouter.go(RouterIntent.this);
            }
        });
    }
}
